package jp.co.cos.ittrick.channel.controller.article;

import jp.co.cos.ittrick.channel.model.ApplicationModel;
import jp.co.cos.ittrick.channel.model.ArticleModel;
import jp.co.cos.ittrick.channel.service.ApplicationService;
import jp.co.cos.ittrick.channel.service.ArticleService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class IndexController extends Controller {

    private ApplicationService applicationService;
    private ArticleService articleService;

    public IndexController() {
        applicationService = new ApplicationService();
        articleService = new ArticleService();
    }

    @Override
    public Navigation run() throws Exception {

        String article = asString("article");

        ApplicationModel applicationModel = applicationService.getApplicationModel();
        requestScope("applicationModel", applicationModel);

        ArticleModel articleModel = null;
        try {
            articleModel = articleService.getArticleByKey(Integer.valueOf(article));
        }catch(Exception e) {
            return forward("/exception/exception404");
        }

        if(articleModel == null) {
            return forward("/exception/exception404");
        }

        requestScope("articleModel", articleModel);

        return forward("article.jsp");
    }
}
